class PermissionsRouter {
	static createRoute(routerList) {
		const router = {
			path: "/index",
			name: "index",
			meta: {
				title: "DemoXY快速开发框架",
				requireAuth: true,
			},
			component: (resolve) => require(["@/views/admin/index"], resolve),
			children: [],
		}
		routerList.forEach((route) => {
			if (route.path) {
				router.children.push({
					path: route.path.substr(route.path.lastIndexOf("/")),
					name: route.path.match(/[^/]+$/)[0],
					// mete: {
					// 	title: `${router.meta.title}-${route.name}`,
					// 	requireAuth: true,
					// },
					component: (resolve) => require([`@/views/admin${route.path}`], resolve),
				})
			}
		})

		// router.children.push({
		// 	path: "*",
		// 	name: "404",
		// 	component: (resolve) => require(["@/views/admin/404"], resolve),
		// 	meta: { title: "DemoXY快速开发框架" },
		// })

		return router
	}
	static menuTree(menuList) {
		const menuRouter = []

		menuList.forEach((menu) => {
			// 查询当前菜单节点的父节点
			const menuParent = menuList.find((row) => row.id == menu.parentId)
			// 如果父节点存在 且
			if (menuParent && !menu.permission) {
				;(menuParent.children || (menuParent.children = [])).push(menu)
			}
		})

		PermissionsRouter.menuRouter(
			menuList.filter((menu) => menu.parentId == "0"),
			menuRouter
		)
		return menuRouter
	}
	// 递归菜单Router化
	static menuRouter(menutree, arr) {
		if (!menutree.length) return
		menutree.forEach((menu) => {
			if (menu?.children?.length) {
				const children = []
				PermissionsRouter.menuRouter(menu.children, children)
				const menuItem = {
					id: menu.id,
					path: menu.path,
					name: menu.name,
					icon: menu.icon,
					children: children,
				}
				arr.push(menuItem)
			} else {
				const menuItem = {
					id: menu.id,
					path: menu.path.substr(menu.path.lastIndexOf("/")),
					name: menu.name,
					icon: menu.icon,
				}
				arr.push(menuItem)
			}
		})
	}
}

export default PermissionsRouter
